package cn.appsys.controller.backend;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.BackendUser;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.pojo.Page;
import cn.appsys.service.AppCategoryService;
import cn.appsys.service.AppInfoService;
import cn.appsys.service.AppVersionService;
import cn.appsys.service.BackedUserService;
import cn.appsys.service.DataDictionaryService;

@Controller
public class BackendUserController {

	@Autowired
	BackedUserService backedUserService;

	@Autowired
	AppInfoService appInfoService;

	@Autowired
	DataDictionaryService dataDictionaryService;

	@Autowired
	AppCategoryService appCategoryService;

	@Autowired
	AppVersionService appVersionService;

	@RequestMapping("/manager/login")
	public String login() {
		return "backendlogin";
	}

	@RequestMapping("/manager/dologin")
	public String dologin(String userCode, String userPassword, Model model, HttpSession httpSession) {
		BackendUser user = backedUserService.login(userCode, userPassword);
		if (user != null) {
			httpSession.setAttribute("userSession", user);
			return "redirect:main";
		}
		model.addAttribute("error", "用户名密码错误");
		return "backendlogin";
	}

	@RequestMapping("/manager/main")
	public String main() {
		return "backend/main";
	}

	@RequestMapping("/manager/backend/app/list")
	public String list(AppInfo appInfo, @RequestParam(defaultValue = "1", required = false) Integer pageIndex,
			Model model) {
		Page<AppInfo> pageInfo = appInfoService.selectAppInfoByPage(appInfo, pageIndex);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("a", appInfo);

		//
		model.addAttribute("statusList", dataDictionaryService.selectDataDictionary("APP_STATUS"));
		model.addAttribute("flatFormList", dataDictionaryService.selectDataDictionary("APP_FLATFORM"));
		model.addAttribute("categoryLevel1List", appCategoryService.selectAppCategory(0L));
		// 初始化选中二级以及三级
		if (appInfo.getCategoryLevel1() != null && appInfo.getCategoryLevel1() > 0)
			model.addAttribute("categoryLevel2List", appCategoryService.selectAppCategory(appInfo.getCategoryLevel1()));
		if (appInfo.getCategoryLevel2() != null && appInfo.getCategoryLevel2() > 0)
			model.addAttribute("categoryLevel3List", appCategoryService.selectAppCategory(appInfo.getCategoryLevel2()));
		return "backend/applist";
	}

	//跳转审核页面
	@RequestMapping("/manager/backend/app/check")
	public String check(Long aid, Long vid, Model model) {
		AppInfo appInfo = appInfoService.getAppInfoById(aid);
		AppVersion appVersion = appVersionService.getAppVersionById(vid);

		model.addAttribute("appInfo", appInfo);
		model.addAttribute("appVersion", appVersion);
		return "backend/appcheck";
	}
	
	//审核
	@RequestMapping("/manager/backend/app/checksave")
	public String checksave(Long id,Integer status,Model model){
		appInfoService.updateStatus(id, status);
		return "redirect:list";
	}

	// 加载分类
	@ResponseBody
	@RequestMapping("/manager/backend/app/categorylevellist")
	public List<AppCategory> categorylevellist(Long pid) {
		return appCategoryService.selectAppCategory(pid);
	}
}
